﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using QxFramework.Core;
using GameControl;
public class RayCannonFunc : ItemFunc
{
    
    public float RayWidth = 0.25f;
    public float BulletLength = 0.5f;
    [Header("两枚弹射击时间间隔")]
    public float ShootGap = 0.5f;
   
    public float BulletSpeed = 10f;
    [Header("子弹在此范围外销毁")]
    public float Range = 20f;
    public float Angle = 270;
  
    public float BulletPower = 199;
    public Color BulletColor = Color.red;
    float lastShootTime = float.NegativeInfinity;

    public override void KeyDownInvoke()
    {
        base.KeyDownInvoke();
        if (Time.time - lastShootTime > ShootGap)
        {
            lastShootTime = Time.time;
            Shoot();
        }
    }
    public override void InvokeByEnmey()
    {
        base.InvokeByEnmey();
        KeyDownInvoke();
    }
    void InitBullet(ItemField item)
    {
        item.Rotation = ThisItem.RotationInDegree;
        item.Position = ThisItem.ItemFieldSetting.Position + ThisItem.GetWorldDirection(Angle) * GameMap.MapManager.itemSize;
        GameManager.Get<IItemHealthManager>().IgnorePairs.UniqueAdd(new Tuple<string, string>(item.ItemName, ThisItem.ItemFieldSetting.ItemName));



        var ray = item as RayItemField;
        ray.RayColor = new Vector3(BulletColor.r,BulletColor.g,BulletColor.b);
        ray.TriggerColliderPoints = RayItem.GetColliderPointsByBounds(new Bounds(Vector2.zero, new Vector3(BulletLength, RayWidth)));
        ray.DeadWhenCollide = true;
        ray.Power = BulletPower;


        existBullets.Add(new Tuple<Vector3, Vector2, string>(item.Position,ThisItem.GetWorldDirection(Angle),item.ItemName));
    }
     List<Tuple<Vector3, Vector2, string>> existBullets = new List<Tuple<Vector3, Vector2, string>>();
    void Shoot()
    {

        MessageManager.Instance.Get<GameMap.ItemMapEvent>().DispatchMessage(GameMap.ItemMapEvent.CreateInMap, this, new GameMap.ItemCreateArgs("Ray", ThisItem.ItemFieldSetting.Position,InitBullet));
    }
    private void FixedUpdate()
    {
        List<string> destoryList = new List<string>();
        foreach(var x in existBullets)
        {
            var item = GameManager.Get<IItemInstantiateManager>().GetItemFieldByName(x.Item3);
            if (item == null)
            {
                destoryList.Add(x.Item3);
                continue;
            }
            var trans = ClockManager.Get<IItemSyncManager>().GetGameObjectByName(x.Item3);
            if (trans == null)
            {
                continue;
            }
            if (Vector2.Distance(trans.transform.position, x.Item1) > Range)
            {
                destoryList.Add(x.Item3);
            }
            else
            {
                trans.transform.position +=(Vector3) x.Item2* BulletSpeed * Time.fixedDeltaTime;
            }
        }
        foreach(var x in destoryList)
        {
            existBullets.RemoveAll(s => s.Item3 == x);
            MessageManager.Instance.Get<GameMap.ItemMapEvent>().DispatchMessage(GameMap.ItemMapEvent.DeleteInMap, this, new GameMap.ItemMapArgs { ItemName =x}, null);
        }
    }
    public override void OnDestory()
    {
        base.OnDestory();
        List<string> destoryList = new List<string>();
        foreach (var x in existBullets)
        {
            destoryList.Add(x.Item3);
        }
        foreach (var x in destoryList)
        {
            existBullets.RemoveAll(s => s.Item3 == x);
            MessageManager.Instance.Get<GameMap.ItemMapEvent>().DispatchMessage(GameMap.ItemMapEvent.DeleteInMap, this, new GameMap.ItemMapArgs { ItemName = x }, null);
        }
    }
}
